#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import datetime
from django.utils import simplejson

from model import Voice


class MyHandler(webapp.RequestHandler):
    
    def get(self):
        voiceCount = [];
        for i in range(10):
            x_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=i * 10);
            plus_one_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=(i+1) * 10);
            voice = db.Query(Voice).filter('when > ', plus_one_minutes_ago).filter('when < ', x_minutes_ago).count()
            voiceCount.append(voice)
            
        # change! - get real number.    
        numOfStuds=33
            
        # change! - get real subjects
        Subjects=[]
        currSubject='Python I'
        nextSubject='JavaScript 101'
        Subjects.append(currSubject)
        Subjects.append(nextSubject)
            
        dirResponse=dict(studentCols=voiceCount,numOfStudents=numOfStuds,subjects=Subjects);    
        
        self.response.out.write(json.dumps(dirResponse))
                            


def main():
    app = webapp.WSGIApplication([
        (r'.*', MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()

##!/usr/bin/env python
#
#import wsgiref.handlers
#from google.appengine.ext import db
#from google.appengine.ext import webapp
#from google.appengine.ext.webapp import template
#from django.utils import simplejson
#import datetime
#from model import Voice
#
#class MyHandler(webapp.RequestHandler):
#    def get(self):
#        self.response.out.write("test");
#        self.
##        self.response.out.write("json page works");
#        voiceCount = ();
#        for i in range(10):
#            x_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=i * 10);
#            plus_one_minutes_ago = datetime.datetime.now() - datetime.timedelta(seconds=(i+1) * 10);
#            voiceCount += (simplejson.loads(
#                        db.Query(Voice)
#                        .filter('when > ', plus_one_minutes_ago)
#                        .filter('when < ', x_minutes_ago)
#                        .count()) , );
##                        
##        
##        #self.response.out.write(simplejson.dumps(voiceCount));